<?php

/**
 * This is the model class for table "{{comment}}".
 *
 * The followings are the available columns in table '{{comment}}':
 * @property integer $comment_id
 * @property integer $comment_post_id
 * @property integer $comment_user_id
 * @property string $comment_author
 * @property string $comment_content
 * @property string $comment_author_email
 * @property string $comment_author_url
 * @property integer $comment_author_ip
 * @property integer $comment_status
 * @property integer $comment_agent
 * @property integer $comment_pid
 * @property integer $created
 * @property integer $modified
 */
class Comment extends CActiveRecord {
    const STATUS_PENDING=1;
    const STATUS_APPROVED=2;
    public $validateCode;
    /**
     * Returns the static model of the specified AR class.
     * @return Comment the static model class
     */
    public static function model($className=__CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return '{{comment}}';
    }

    /**
     * add default created modified time
     */

    public function behaviors() {
        return array(
                'AutoTimestampBehavior' => array(
                        'class' => 'application.components.AutoTimestampBehavior',
                //You can optionally set the field name options here
                )
        );
    }
    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
                array('comment_post_id, validateCode, comment_author, comment_author_email, comment_content,  comment_author_ip', 'required'),
                array('comment_post_id,  comment_author_ip', 'numerical', 'integerOnly'=>true),
                //array('comment_author, comment_author_email, comment_author_url', 'length', 'max'=>200),
                array('comment_author_url','safe'),
                array('validateCode', 'captcha', 'allowEmpty'=>!CCaptcha::checkRequirements()),
                // The following rule is used by search().
                // Please remove those attributes that should not be searched.
                array('comment_id, comment_post_id, comment_user_id, comment_author, comment_content, comment_author_email, comment_author_url, comment_author_ip, comment_status, comment_agent, comment_pid, created, modified', 'safe', 'on'=>'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
                'post' => array(self::BELONGS_TO, 'Posts', 'comment_post_id'),
        );
    }

    /**
     * add default desc
     */

    public function scopes() {
        return array(
                'published'=>array(
                        'order'=>'created desc',
                ),
        );

    }
    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
                'comment_id' => 'Comment',
                'comment_post_id' => 'Comment Post',
                'comment_user_id' => 'Comment User',
                'comment_author' => Yii::t('index','Comment Author'),
                'comment_content' => Yii::t('index','Comment Content'),
                'comment_author_email' => Yii::t('index','Comment Author Email'),
                'comment_author_url' => Yii::t('index','Comment Author Url'),
                'comment_author_ip' => Yii::t('index','Comment Author Ip'),
                'comment_status' => Yii::t('index','Comment Status'),
                'comment_agent' => Yii::t('index','Comment Agent'),
                'comment_pid' => Yii::t('index','Comment Pid'),
                'validateCode'=>Yii::t('index','validateCode'),
                'created' => 'Created',
                'modified' => 'Modified',
        );
    }
    /**
     * @param Post the post that this comment belongs to. If null, the method
     * will query for the post.
     * @return string the permalink URL for this comment
     */
    public function getUrl($post=null) {
        if($post===null)
            $post=$this->post;
        return $post->url.'#c'.$this->comment_id;
    }

    public function getGravurl() {
        //$default = "http://www.somewhere.com/homestar.jpg";
        $size=40;
        $grav_url = "http://www.gravatar.com/avatar/" . md5( strtolower( trim( $this->comment_author_email ) ) ) . "?s=" . $size;
        return $grav_url;
    }

    public function getLink($post=null) {
        if($post===null) {
            $post=$this->post;
            $url= $post['url'].'#c'.$this->comment_id;
        }
        return  CHtml::link(CHtml::encode($this->comment_content),$url);
    }
    /**
     * @return string the hyperlink display for the current comment's author
     */
    public function getAuthorLink() {
        if(!empty($this->url))
            return CHtml::encode($this->comment_author);
        //return CHtml::link(CHtml::encode($this->comment_author),$this->comment_author_url);
        else
            return CHtml::encode($this->comment_author);
    }

    public function getUrllink() {
        if(!empty($this->comment_author_url))
        //return CHtml::encode($this->comment_author);
            return CHtml::link(CHtml::encode($this->comment_author_url),$this->comment_author_url,array('target'=>'_blank','rel'=>'nofollow'));
        else
            return "";
//            return CHtml::encode($this->comment_author);
    }
    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria=new CDbCriteria;
        $criteria->order="created desc";
        $criteria->compare('comment_id',$this->comment_id);
        $criteria->compare('comment_post_id',$this->comment_post_id);
        $criteria->compare('comment_user_id',$this->comment_user_id);
        $criteria->compare('comment_author',$this->comment_author,true);
        $criteria->compare('comment_content',$this->comment_content,true);
        $criteria->compare('comment_author_email',$this->comment_author_email,true);
        $criteria->compare('comment_author_url',$this->comment_author_url,true);
        $criteria->compare('comment_author_ip',$this->comment_author_ip);
        $criteria->compare('comment_status',$this->comment_status);
        $criteria->compare('comment_agent',$this->comment_agent);
        $criteria->compare('comment_pid',$this->comment_pid);
        $criteria->compare('created',$this->created);
        $criteria->compare('modified',$this->modified);

        return new CActiveDataProvider(get_class($this), array(
                        'criteria'=>$criteria,
        ));
    }
}